home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 98 / CD-ROM 98.iso / infantil / tuxmath / tuxmath-2001.09.07-win32-installer.exe / src / title.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-01  |  6.4 KB  |  308 lines

  1. /*
  2.   title.c
  3.  
  4.   For TuxMath
  5.   The title screen function.
  6.  
  7.   by Bill Kendrick
  8.   bill@newbreedsoftware.com
  9.   http://www.newbreedsoftware.com/
  10.  
  11.  
  12.   Part of "Tux4Kids" Project
  13.   http://www.tux4kids.org/
  14.       
  15.   August 26, 2001 - August 31, 2001
  16. */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <SDL.h>
  23. #include "title.h"
  24. #include "images.h"
  25. #include "setup.h"
  26. #include "sounds.h"
  27. #include "playsound.h"
  28.  
  29.  
  30. #define START_DEMO_COUNTDOWN 150  /* Some time unit.. not sure yet :) */
  31.  
  32.  
  33. int title(void)
  34. {
  35.   int i, cmd, old_cmd, done, img, blinking, widest, left, width, demo_countdown;
  36.   SDL_Rect dest;
  37.   SDL_Event event;
  38.   Uint32 last_time, now_time;
  39.   SDLKey key;
  40.   
  41.  
  42.   /* Determine widest option image size: */
  43.  
  44.   widest = 0;
  45.   for (i = 0; i < NUM_CMDS; i++)
  46.     {
  47.       if (images[IMG_CMD_PLAY + i]->w > widest)
  48.         widest = images[IMG_CMD_PLAY + i]->w;
  49.     }
  50.  
  51.   width = widest + 4 + 4 + images[IMG_TUX_HELMET1] -> w;
  52.   left = (screen->w - width) / 2;
  53.   
  54.   
  55.   /* Clear window: */
  56.   
  57.   SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  58.   
  59.   
  60.   /* Draw title: */
  61.   
  62.   dest.x = (screen->w - images[IMG_TITLE]->w) / 2;
  63.   dest.y = 0;
  64.   dest.w = images[IMG_TITLE]->w;
  65.   dest.h = images[IMG_TITLE]->h;
  66.   
  67.   SDL_BlitSurface(images[IMG_TITLE], NULL, screen, &dest);
  68.  
  69.  
  70.   /* Draw version number: */
  71.  
  72.   dest.x = (screen->w - images[IMG_ALPHA]->w) / 2;
  73.   dest.y = 0;
  74.   dest.w = images[IMG_ALPHA]->w;
  75.   dest.h = images[IMG_ALPHA]->h;
  76.  
  77.   SDL_BlitSurface(images[IMG_ALPHA], NULL, screen, &dest);
  78.  
  79.   
  80.   
  81.   /* Draw options: */
  82.   
  83.   for (i = 4; i >= 0; i--)
  84.     {
  85.       // dest.x = (32 - 2) - i;
  86.       dest.x = left + 4 - i;
  87.       dest.y = (images[IMG_TITLE]->h + 2) + (4 - i);
  88.       // dest.w = (screen->w) - ((32 - 2) * 2) + (i * 2);
  89.       dest.w = width + (i * 2);
  90.       dest.h = (NUM_CMDS * images[IMG_TUX_HELMET1]->h + 2) + (i * 2);
  91.       
  92.       SDL_FillRect(screen, &dest,
  93.            SDL_MapRGB(screen->format,
  94.                   200 - (i * 32),
  95.                   232 - (i * 32),
  96.                   255 - (i * 32)));
  97.     }
  98.   
  99.   
  100.   for (i = 0; i < NUM_CMDS; i++)
  101.     {
  102.       // dest.x = 32 + (images[IMG_TUX_HELMET1]->w + 4);
  103.       dest.x = left + (images[IMG_TUX_HELMET1]->w) + 4;
  104.       dest.y = (images[IMG_TITLE]->h + 8 + 
  105.         (i * images[IMG_TUX_HELMET1]->h));
  106.       dest.w = images[IMG_TUX_HELMET1]->w;
  107.       dest.h = images[IMG_TUX_HELMET1]->h;
  108.       
  109.       SDL_BlitSurface(images[IMG_CMD_PLAY + i], NULL, screen, &dest);
  110.     }
  111.   
  112.   
  113.   /* Draw "Tux4Kids" logo: */
  114.   
  115.   dest.x = (screen->w - images[IMG_TUX4KIDS]->w);
  116.   dest.y = (screen->h - images[IMG_TUX4KIDS]->h);
  117.   dest.w = images[IMG_TUX4KIDS]->w;
  118.   dest.h = images[IMG_TUX4KIDS]->h;
  119.   
  120.   SDL_BlitSurface(images[IMG_TUX4KIDS], NULL, screen, &dest);
  121.  
  122.  
  123.   /* Draw "New Breed Software" logo: */
  124.  
  125.   dest.x = 0;
  126.   dest.y = (screen->h - images[IMG_NBS]->h);
  127.   dest.w = images[IMG_NBS]->w;
  128.   dest.h = images[IMG_NBS]->h;
  129.  
  130.   SDL_BlitSurface(images[IMG_NBS], NULL, screen, &dest);
  131.  
  132.  
  133.   if (demo_mode)
  134.   {
  135.     dest.x = (screen->w - images[IMG_DEMO_SMALL]->w) / 2;
  136.     dest.y = (screen->h - images[IMG_DEMO_SMALL]->h);
  137.     dest.w = images[IMG_DEMO_SMALL]->w;
  138.     dest.h = images[IMG_DEMO_SMALL]->h;
  139.  
  140.     SDL_BlitSurface(images[IMG_DEMO_SMALL], NULL, screen, &dest);
  141.   }
  142.  
  143.   
  144.   /* Flip the screen: */
  145.   
  146.   SDL_Flip(screen);
  147.   
  148.   
  149.   
  150.   /* --- MAIN TITLE SCREEN LOOP: --- */
  151.  
  152.   blinking = 0;
  153.   cmd = 0;
  154.   done = 0;
  155.   demo_countdown = START_DEMO_COUNTDOWN;
  156.   
  157.   do
  158.     {
  159.       last_time = SDL_GetTicks();
  160.       old_cmd = cmd;
  161.       
  162.       
  163.       /* Handle any incoming events: */
  164.       
  165.       while (SDL_PollEvent(&event) > 0)
  166.     {
  167.       if (event.type == SDL_QUIT)
  168.         {
  169.           /* Window close event - quit! */
  170.           
  171.           cmd = CMD_QUIT;
  172.           done = 1;
  173.         }
  174.       else if (event.type == SDL_KEYDOWN)
  175.         {
  176.           key = event.key.keysym.sym;
  177.           
  178.           if (key == SDLK_ESCAPE)
  179.         {
  180.           /* Escape key - quit! */
  181.           
  182.           cmd = CMD_QUIT;
  183.           done = 1;
  184.         }
  185.           else if (key == SDLK_DOWN)
  186.         {
  187.           demo_countdown = START_DEMO_COUNTDOWN;
  188.           
  189.           cmd++;
  190.           
  191.           if (cmd >= NUM_CMDS)
  192.             cmd = NUM_CMDS - 1;
  193.         }
  194.           else if (key == SDLK_UP)
  195.         {
  196.           demo_countdown = START_DEMO_COUNTDOWN;
  197.  
  198.           cmd--;
  199.           
  200.           if (cmd < 0)
  201.             cmd = 0;
  202.         }
  203.           else if (key == SDLK_RETURN)
  204.         {
  205.           done = 1;
  206.         }
  207.         }
  208.       else if (event.type == SDL_MOUSEBUTTONDOWN)
  209.       {
  210.             /* Mouse click: */
  211.     
  212.         if (event.button.x >= left &&
  213.             event.button.x <= left + width &&
  214.         event.button.y >= images[IMG_TITLE]->h + 8 &&
  215.         event.button.y <= (images[IMG_TITLE]->h + 8 +
  216.                            NUM_CMDS * images[IMG_TUX_HELMET1]->h))
  217.         {
  218.           cmd = ((event.button.y - (images[IMG_TITLE]->h + 8)) /
  219.                  images[IMG_TUX_HELMET1]->h);
  220.  
  221.           done = 1;
  222.         }
  223.       }
  224.     }
  225.       
  226.       
  227.       /* Erase Tux (cursor) */
  228.       
  229.       if (cmd != old_cmd)
  230.     {
  231.       blinking = 0;
  232.       
  233.       
  234.       dest.x = left + 4;
  235.       // dest.x = 32;
  236.       dest.y = (images[IMG_TITLE]->h + 8 + 
  237.             (old_cmd * images[IMG_TUX_HELMET1]->h));
  238.       dest.w = images[IMG_TUX_HELMET1]->w;
  239.       dest.h = images[IMG_TUX_HELMET1]->h;
  240.       
  241.       SDL_FillRect(screen, &dest,
  242.                SDL_MapRGB(screen->format, 200, 232, 255));
  243.  
  244.       playsound(SND_POP);
  245.     }
  246.       
  247.       
  248.       /* Handling Tux (cursor) blinking: */
  249.       
  250.       if ((rand() % 50) == 0 && blinking == 0)
  251.     {
  252.       blinking = 6;
  253.     }
  254.       
  255.       if (blinking > 0)
  256.     blinking--;
  257.       
  258.       
  259.       /* Draw Tux (cursor) */
  260.       
  261.       // dest.x = 32;
  262.       dest.x = left + 4;
  263.       dest.y = images[IMG_TITLE]->h + 8 + (cmd * images[IMG_TUX_HELMET1]->h);
  264.       dest.w = images[IMG_TUX_HELMET1]->w;
  265.       dest.h = images[IMG_TUX_HELMET1]->h;
  266.       
  267.       img = IMG_TUX_HELMET1;
  268.       
  269.       if (blinking >= 4 || (blinking >= 1 && blinking < 2))
  270.     img = IMG_TUX_HELMET2;
  271.       else if (blinking >= 2 && blinking < 4)
  272.     img = IMG_TUX_HELMET3;
  273.       
  274.       SDL_BlitSurface(images[img], NULL, screen, &dest);
  275.  
  276.       SDL_Flip(screen);
  277.  
  278.  
  279.       /* Handle demo countdown: */
  280.  
  281.       if (demo_mode)
  282.       {
  283.     demo_countdown--;
  284.  
  285.     if (demo_countdown == 0)
  286.     {
  287.       cmd = CMD_GAME;
  288.       done = 1;
  289.     }
  290.       }
  291.  
  292.       
  293.       /* Pause (keep frame-rate event) */
  294.       
  295.       now_time = SDL_GetTicks();
  296.       if (now_time < last_time + (1000 / 20))
  297.     {
  298.       SDL_Delay(last_time + (1000 / 20) - now_time);
  299.     }
  300.     }
  301.   while (!done);
  302.   
  303.   
  304.   /* Return the chosen command: */
  305.   
  306.   return cmd;
  307. }
  308.